package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjPendingBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjPendingMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;


public class PjPendingRecover implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjPendingBean pjPending = JsonUtils.objectToPojo(req.getData(), PjPendingBean.class);
            if (StringUtils.isBlank(pjPending.getPjId())){
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "pjId 不能为空", Void.class);
            }
            PjPendingMapper pjPendingMapper = factory.getMapper(PjPendingMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);

            pjPending = pjPendingMapper.selectByPjId(pjPending.getPjId());

            PjInfoBean pjInfo = pjInfoMapper.selectById(pjPending.getPjId());
            pjInfo.setPendingState(0);
            pjInfo.setProcessState(pjPending.getPjProcessState());
            pjInfoMapper.update(pjInfo);

            pjPendingMapper.updatePendingState(pjPending.getId(), 3);

            logger.info("工程编号：{} 成功恢复", pjInfo.getBillNo());

            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "执行成功", null);
        } catch (Exception e) {
            logger.error("执行失败", e);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "执行失败", null);
    }
}
